package com.rc.saas.mini.ali;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.rc.saas.mini.component.MiniExceptionsHandler;
import com.rc.saas.mini.component.RedisLoader;
import com.rc.saas.mini.model.member.MemberInfo;
import com.rc.saas.mini.model.tenant.Tenant;
import com.rc.saas.mini.service.tenant.TenantService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Author: zcg
 * @CreateDate: 2022/4/28 12:35
 */
@Component
public class AliUtil {
    private static Logger logger = LogManager.getLogger(MiniExceptionsHandler.class);

    @Autowired
    private RedisLoader redisLoader;


    //换取授权访问令牌)
    public AlipaySystemOauthTokenResponse getAliToken(String code, Tenant tenant) throws AlipayApiException {
        AlipaySystemOauthTokenResponse response = redisLoader.opsForValueGet("ali:" + code, AlipaySystemOauthTokenResponse.class);
        if (tenant != null) {
            if (response != null) {
                logger.info("refresh token");
                //未过期
                AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", tenant.getAliAppid(), tenant.getAliSecret(), "json", "GBK",
                        //"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAppEFjH8bI0nh4UZZ6NZbBkUutcqt/4WSDbGONkhj8mKiytfssmLYvhd945d5stvzWoqNkDYbGuHMmzvT8yJPuk+43WIOm0l9TZxeHr0KMpfb9jmr4dClH9odNVL5J/haEKdepHH2oJhlfl2/VBsWKEhue5MIDiCvhC/7UwVX5m6EYZIEJdFMOptTkWyXJDIGigBmM7gM6/AVqalY2AXebBcMNBkmoubB+h4tu4cloD00HCxzjps9ixdtG8Se9Tv7s1EhL6th5/xyy2ykqyEUBNGgAeeE7EbDwPE9J4hy/6NvnpZyQhiKk6yOlRxUBmEsN2WLmEVwupx6ptr8PHPlRQIDAQAB",
                        //"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAppEFjH8bI0nh4UZZ6NZbBkUutcqt/4WSDbGONkhj8mKiytfssmLYvhd945d5stvzWoqNkDYbGuHMmzvT8yJPuk+43WIOm0l9TZxeHr0KMpfb9jmr4dClH9odNVL5J/haEKdepHH2oJhlfl2/VBsWKEhue5MIDiCvhC/7UwVX5m6EYZIEJdFMOptTkWyXJDIGigBmM7gM6/AVqalY2AXebBcMNBkmoubB+h4tu4cloD00HCxzjps9ixdtG8Se9Tv7s1EhL6th5/xyy2ykqyEUBNGgAeeE7EbDwPE9J4hy/6NvnpZyQhiKk6yOlRxUBmEsN2WLmEVwupx6ptr8PHPlRQIDAQAB",
                        "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgHOwYg8CNp6x4ZLo1uvsZ3TVWHiMUatfrHTi7bZbHUBLHN92Z3i8Iqkq+bN3MbkoSeBB4/YDOPqeMfTONZ2BUIMgFeB5TOLsn0eIZKA4ZllsawKMdOG9ZCXryfX0IOY0kiDGmkYzfWtg+8HPsBF4zdvuKTmU1lhW7iDROjPu+R3zwHjx70h+eQ8bxcGLeKO5tNOZC5lkcdeIO7Rph6Y3yHpsUWhbs5yuyd8W3WBWS3YZsevqraYDt6SkZHd0V4SaxIN6mG1Q9p0om3qJappcIs1pXu0EqkYhHi0us5jhfrwgM4dS5+tNxMKhpxcoQ1S+gFppBqb+8YHJV6eYdLg3YQIDAQAB",
                        "RSA2");
                AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
                request.setGrantType("refresh_token");
                //request.setCode(code);
                request.setRefreshToken(response.getRefreshToken());
                response = alipayClient.execute(request);
                if (response.isSuccess()) {
                    System.out.println("调用成功");
                    return response;
                } else {
                    System.out.println("调用失败");
                }
            }
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", tenant.getAliAppid(), tenant.getAliSecret(), "json", "GBK",
                    //"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAppEFjH8bI0nh4UZZ6NZbBkUutcqt/4WSDbGONkhj8mKiytfssmLYvhd945d5stvzWoqNkDYbGuHMmzvT8yJPuk+43WIOm0l9TZxeHr0KMpfb9jmr4dClH9odNVL5J/haEKdepHH2oJhlfl2/VBsWKEhue5MIDiCvhC/7UwVX5m6EYZIEJdFMOptTkWyXJDIGigBmM7gM6/AVqalY2AXebBcMNBkmoubB+h4tu4cloD00HCxzjps9ixdtG8Se9Tv7s1EhL6th5/xyy2ykqyEUBNGgAeeE7EbDwPE9J4hy/6NvnpZyQhiKk6yOlRxUBmEsN2WLmEVwupx6ptr8PHPlRQIDAQAB",
                    //"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAppEFjH8bI0nh4UZZ6NZbBkUutcqt/4WSDbGONkhj8mKiytfssmLYvhd945d5stvzWoqNkDYbGuHMmzvT8yJPuk+43WIOm0l9TZxeHr0KMpfb9jmr4dClH9odNVL5J/haEKdepHH2oJhlfl2/VBsWKEhue5MIDiCvhC/7UwVX5m6EYZIEJdFMOptTkWyXJDIGigBmM7gM6/AVqalY2AXebBcMNBkmoubB+h4tu4cloD00HCxzjps9ixdtG8Se9Tv7s1EhL6th5/xyy2ykqyEUBNGgAeeE7EbDwPE9J4hy/6NvnpZyQhiKk6yOlRxUBmEsN2WLmEVwupx6ptr8PHPlRQIDAQAB",
                    "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgHOwYg8CNp6x4ZLo1uvsZ3TVWHiMUatfrHTi7bZbHUBLHN92Z3i8Iqkq+bN3MbkoSeBB4/YDOPqeMfTONZ2BUIMgFeB5TOLsn0eIZKA4ZllsawKMdOG9ZCXryfX0IOY0kiDGmkYzfWtg+8HPsBF4zdvuKTmU1lhW7iDROjPu+R3zwHjx70h+eQ8bxcGLeKO5tNOZC5lkcdeIO7Rph6Y3yHpsUWhbs5yuyd8W3WBWS3YZsevqraYDt6SkZHd0V4SaxIN6mG1Q9p0om3qJappcIs1pXu0EqkYhHi0us5jhfrwgM4dS5+tNxMKhpxcoQ1S+gFppBqb+8YHJV6eYdLg3YQIDAQAB",
                    "RSA2");
            AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
            request.setGrantType("authorization_code");
            request.setCode(code);
            //request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
            response = alipayClient.execute(request);
            if (response.isSuccess()) {
                System.out.println("调用成功");
                redisLoader.opsForValueSet("ali:" + code, response, Integer.valueOf(response.getExpiresIn()), TimeUnit.SECONDS);
            } else {
                System.out.println("调用失败");
            }
        }
        return response;
    }

    //支付宝会员授权信息查询接口
    public AlipayUserInfoShareResponse changeAliMembeInfo(String code, Tenant tenant) throws AlipayApiException {

        AlipaySystemOauthTokenResponse alipaySystemOauthTokenResponse = getAliToken(code, tenant);

        AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", tenant.getAliAppid(),
                "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCmkQWMfxsjSeHhRlno1lsGRS61yq3/hZINsY42SGPyYqLK1+yyYti+F33jl3my2/Naio2QNhsa4cybO9PzIk+6T7jdYg6bSX1NnF4evQoyl9v2Oavh0KUf2h01Uvkn+FoQp16kcfagmGV+Xb9UGxYoSG57kwgOIK+EL/tTBVfmboRhkgQl0Uw6m1ORbJckMgaKAGYzuAzr8BWpqVjYBd5sFww0GSai5sH6Hi27hyWgPTQcLHOOmz2LF20bxJ71O/uzUSEvq2Hn/HLLbKSrIRQE0aAB54TsRsPA8T0niHL/o2+elnJCGIqTrI6VHFQGYSw3ZYu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QD1MWtRBj+mf3Bla9qYZLdoADpSWdlWE/w0mwROxnP7ouZGBzNpPDhA++Vz63x6+NyvySRn5yaBPYkjhUpipaVzkA0VG1qKtJA5HuxurT5p0W/RiS7e6TWQrYtqC/jblKZ5tb9WqRoi+Uda8RgJ+aQLNBVR00xK+uUkvnv+nIfg0g==",
                "json", "GBK",
                //"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAppEFjH8bI0nh4UZZ6NZbBkUutcqt/4WSDbGONkhj8mKiytfssmLYvhd945d5stvzWoqNkDYbGuHMmzvT8yJPuk+43WIOm0l9TZxeHr0KMpfb9jmr4dClH9odNVL5J/haEKdepHH2oJhlfl2/VBsWKEhue5MIDiCvhC/7UwVX5m6EYZIEJdFMOptTkWyXJDIGigBmM7gM6/AVqalY2AXebBcMNBkmoubB+h4tu4cloD00HCxzjps9ixdtG8Se9Tv7s1EhL6th5/xyy2ykqyEUBNGgAeeE7EbDwPE9J4hy/6NvnpZyQhiKk6yOlRxUBmEsN2WLmEVwupx6ptr8PHPlRQIDAQAB",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgHOwYg8CNp6x4ZLo1uvsZ3TVWHiMUatfrHTi7bZbHUBLHN92Z3i8Iqkq+bN3MbkoSeBB4/YDOPqeMfTONZ2BUIMgFeB5TOLsn0eIZKA4ZllsawKMdOG9ZCXryfX0IOY0kiDGmkYzfWtg+8HPsBF4zdvuKTmU1lhW7iDROjPu+R3zwHjx70h+eQ8bxcGLeKO5tNOZC5lkcdeIO7Rph6Y3yHpsUWhbs5yuyd8W3WBWS3YZsevqraYDt6SkZHd0V4SaxIN6mG1Q9p0om3qJappcIs1pXu0EqkYhHi0us5jhfrwgM4dS5+tNxMKhpxcoQ1S+gFppBqb+8YHJV6eYdLg3YQIDAQAB",
                "RSA2");
        AlipayUserInfoShareResponse response = alipayClient.execute(request, alipaySystemOauthTokenResponse.getAccessToken());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response;
    }
}
